import { loginApi } from '../../utils/serviceApiUrl.js'
import storageHelper from '../../utils/storageHelper'
const USER_INFO = 'USER_INFO'
const state = {
	userInfo: null,
}

const getters = {
	userInfo(state) {
		if (state.userInfo) {
			return state.userInfo
		} else {
			return storageHelper.get('safe_company_userInfo')
		}
	},
}

const actions = {
	loginSen({ commit }, param) {
		return new Promise((resolve) => {
			loginApi(param).then((res) => {
				commit(USER_INFO, res.employeeInfo)
				console.log('reslogin', res)
				storageHelper.set('mabangerp_userInfo', res.employeeInfo)
				storageHelper.set('mabangerp_token', res.employeeInfo.token)
				resolve(res)
			})
		})
	},
}
const mutations = {
	[USER_INFO](state, data) {
		state.userInfo = data
	},
}

export default {
	state,
	getters,
	actions,
	mutations,
}
